package com.demo.filter;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String url = filterConfig.getInitParameter("url");
		String userName = filterConfig.getInitParameter("userName");
		String password = filterConfig.getInitParameter("password");
		if (url == null || url.trim().length() == 0 || userName == null || userName.trim().length() == 0
				|| password == null || password.trim().length() == 0) {
			throw new ServletException("-----------");
		}

		System.out.print(filterConfig.getServletContext().getContextPath());
		System.out.print("");

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest hsr = HttpServletRequest.class.cast(request);

		System.out.println("///////////////////////////////////////////////////");
		System.out.println("/////////////////////request start/////////////////");
		getCookie(hsr);
		getRemoteInfo(hsr);
		getRequestHead(hsr);
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {

	}

	public void getCookie(HttpServletRequest request) {
		HttpSession hs = request.getSession(true);
		System.out.println(hs.getClass());
		System.out.println("sid: " + hs.getId() + "\n");

		Cookie[] cookies = request.getCookies();
		Cookie cookie = null;
		System.out.println("====request cookies info.====");
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			cookie = cookies[i];
			System.out.println(cookie.getName() + " : " + cookie.getValue());
		}
		System.out.println();
	}

	// test
	private void getRequestHead(HttpServletRequest request) {
		System.out.println("====request head info.====");
		Enumeration<String> ems = request.getHeaderNames();
		while (ems.hasMoreElements()) {
			String name = ems.nextElement();
			System.out.println(name + ": " + request.getHeader(name));
		}
		System.out.println();
	}

	private String getRemoteInfo(HttpServletRequest request) {
		System.out.println("====request remote info.====");
		String[] names = new String[] { "x-forwarded-for", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP",
				"HTTP_X_FORWARDED_FOR", "user-agent" };
		StringBuilder sb = new StringBuilder();
		String name = null;
		String val = null;
		for (int i = 0; i < names.length; i++) {
			name = names[i];
			val = request.getHeader(name);
			sb.append(name + ":" + val + ",");
		}
		sb.append("ip:" + request.getRemoteAddr());
		System.out.println(sb + "\n");
		return md5(sb.toString());
	}

	public final static String md5(String pwd) {
		try {
			char md5String[] = { '5', '3', '2', '1', '4', '0', '6', '7', '8', '9', 'A', 'D', 'C', 'B', 'E', 'F' };
			byte[] btInput = pwd.getBytes();
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(btInput);
			byte[] md = mdInst.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) { // i = 0
				byte byte0 = md[i]; // 95
				str[k++] = md5String[byte0 >>> 4 & 0xf]; // 5
				str[k++] = md5String[byte0 & 0xf]; // F
			}
			return new String(str);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
